package com.ghck.shop.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ghck.server.exception.ErrorConstant;
import com.ghck.shop.config.ConfigUtils;
import com.ghck.shop.dao.GoodsSpecificationMapper;
import com.ghck.shop.dao.ShoppingCartMapper;
import com.ghck.shop.exception.ShopServerException;
import com.ghck.shop.model.GoodsConstant;
import com.ghck.shop.model.GoodsSpecification;
import com.ghck.shop.model.request.ShoppingCartReq;
import com.ghck.shop.model.response.GoodsRes;
import com.ghck.shop.model.response.ShoppingCartRes;
import com.ghck.shop.service.GoodsService;
import com.ghck.shop.service.ShoppingCartService;
import com.ghck.shop.utils.FileStoreUtil;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService{

	@Autowired
	private ShoppingCartMapper shoppingCartMapper;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private GoodsSpecificationMapper goodsSpecificationMapper;
	
	@Override
	public ShoppingCartReq addShoppingCart(ShoppingCartReq shoppingCartReq) {
		if(shoppingCartReq.getSpecificationId() == null){//如果规格不存在 规格置为0
			shoppingCartReq.setSpecificationId(0);
		}
		if(shoppingCartReq.getGoodsId() == null || shoppingCartReq.getGoodsId() <= 0){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "商品ID不能为空");
		}
		if(shoppingCartReq.getGoodsNum()== null || shoppingCartReq.getGoodsNum() <= 0){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "商品数量不能为空");
		}
		if(shoppingCartReq.getUserId() == null || shoppingCartReq.getUserId() <= 0){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "用户ID不能为空");
		}
		GoodsRes goods = goodsService.goodsDetailById(shoppingCartReq.getGoodsId());
		GoodsSpecification goodsSpecification = null;
		if(shoppingCartReq.getSpecificationId() > 0){
			goodsSpecification = goodsSpecificationMapper.findGoodsSpecificationById(shoppingCartReq.getSpecificationId());
			if(goodsSpecification == null){
				throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "商品规格不存在");
			}
		}
		if(goodsSpecification != null ){//如果有规格需要判断规格的库存和规格的状态
			if(goodsSpecification.getAmount().intValue() < shoppingCartReq.getGoodsNum().intValue()){
				throw new ShopServerException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "库存仅剩"+goodsSpecification.getAmount()+"件");
			}
			if(goodsSpecification.getStatus() != 1){
				throw new ShopServerException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "该规格不可用");
			}
		}else {
			if(goods.getAmount().intValue() < shoppingCartReq.getGoodsNum().intValue()){
				throw new ShopServerException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "库存仅剩"+goods.getAmount()+"件");
			}
		}
		if(goods.getIsOnsale() == GoodsConstant.GOODS_ONSALE_STATUS_NOT.getStatus()){
			throw new ShopServerException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "加入购物车失败,商品未上架");
		}
		if(goods.getIsOnsale() == GoodsConstant.GOODS_ONSALE_STATUS_UNDERCARRIAGE.getStatus()){
			throw new ShopServerException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "加入购物车失败,商品已下架");
		}
		
		ShoppingCartReq scr = shoppingCartMapper.findShoppingCartByGoodsIdAndUserId(shoppingCartReq);
		//如果存在更新购物车数量
		if(null != scr){
			shoppingCartMapper.updateNum(shoppingCartReq);
			shoppingCartReq.setGoodsNum(scr.getGoodsNum() + shoppingCartReq.getGoodsNum());
		}else{
			//不存在新增
			shoppingCartMapper.insert(shoppingCartReq);
		}
		return shoppingCartReq;
	}

	@Override
	public void deleteShoppingCart(ShoppingCartReq shoppingCartReq) {
		
		if(shoppingCartReq.getId() == null){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "购物车ID不能为空");
		}
		if(shoppingCartReq.getUserId() == null || shoppingCartReq.getUserId() <= 0){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "用户ID不能为空");
		}
		if(shoppingCartReq.getSpecificationId() == null){//如果规格不存在 规格置为0
			shoppingCartReq.setSpecificationId(0);
		}
		ShoppingCartReq scr = shoppingCartMapper.findShoppingCartByGoodsIdAndUserId(shoppingCartReq);
		if(null != scr && scr.getUserId().intValue() != shoppingCartReq.getUserId().intValue()){
			if(shoppingCartReq.getUserId() == null || shoppingCartReq.getUserId() <= 0){
				throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "该购物车商品不属于您,不能删除");
			}
		}
		shoppingCartMapper.delete(shoppingCartReq.getId());
	}

	@Override
	public void updateShoppingCart(Integer userId,ShoppingCartReq[] shoppingCartReq) {
		if(null == shoppingCartReq || shoppingCartReq.length == 0){
			return ;
		}
		if(userId == null || userId <= 0){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "用户ID不能为空");
		}
		for(ShoppingCartReq scr : shoppingCartReq){
			if(null != scr && null != scr.getGoodsId()){
				if(scr.getSpecificationId() == null){//如果规格不存在 规格置为0
					scr.setSpecificationId(0);
				}
				scr.setUserId(userId);
				shoppingCartMapper.update(scr);
			}
		}
	}
	
	@Override
	public Integer getShoppingCartNum(ShoppingCartReq shoppingCartReq) {
		if(shoppingCartReq.getUserId() == null){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "用户ID不能为空");
		}
		if(shoppingCartReq.getSpecificationId() == null){//如果规格不存在 规格置为0
			shoppingCartReq.setSpecificationId(0);
		}
		return shoppingCartMapper.findShoppingCartNum(shoppingCartReq);
	}

	@Override
	public List<ShoppingCartRes> getShoppingCartList(ShoppingCartReq shoppingCartReq) {
		if(shoppingCartReq.getUserId() == null){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(), "用户ID不能为空");
		}
		if(shoppingCartReq.getSpecificationId() == null){//如果规格不存在 规格置为0
			shoppingCartReq.setSpecificationId(0);
		}
		List<ShoppingCartRes> ShoppingCartList = shoppingCartMapper.findShoppingCartList(shoppingCartReq);
		List<Integer> specificationIds = new ArrayList<Integer>();
		for(ShoppingCartRes scr : ShoppingCartList){
			if(scr.getSpecificationId() != null && scr.getSpecificationId() > 0){
				specificationIds.add(scr.getSpecificationId());
			}
			if(StringUtils.isNotEmpty(scr.getThumbnailImage())){
				scr.setThumbnailImage(FileStoreUtil.getBaseUrl().concat(scr.getThumbnailImage())); 
			}
		}
		if(CollectionUtils.isNotEmpty(specificationIds)){
			List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.findGoodsSpecificationsByIds(specificationIds); 
			Map<Integer, GoodsSpecification> goodsSpecificationMap = convertGoodsSpecification2Map(goodsSpecifications);
			for(ShoppingCartRes scr : ShoppingCartList){
				if(scr.getSpecificationId() != null && scr.getSpecificationId() > 0){
					if(goodsSpecificationMap.containsKey(scr.getSpecificationId())){
						GoodsSpecification goodsSpecification = goodsSpecificationMap.get(scr.getSpecificationId());
						scr.setSalePrice(goodsSpecification.getSalePrice());
						scr.setSpecificationName(goodsSpecification.getName());
						scr.setAmount(goodsSpecification.getAmount());
					}
				}
			}
		}
		return ShoppingCartList;
	}

	private Map<Integer, GoodsSpecification> convertGoodsSpecification2Map(List<GoodsSpecification> goodsSpecifications) {
		Map<Integer, GoodsSpecification> map = new HashMap<Integer, GoodsSpecification>();
		for(GoodsSpecification goodsSpecification:goodsSpecifications){
			Integer id = goodsSpecification.getId();
			GoodsSpecification temp = map.get(id);
			if(temp == null){
				map.put(id, goodsSpecification);
			}
		}
		return map;
	}
}
